<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
        html, body {
           background-color: #f8f8f8; 
        }
        .sort_box {
        	border-bottom: 1px solid #dadada;
        	background-color: #ffffff;
        	margin-bottom: 6px;
            position: fixed;
            top: 0;
            width: 100%;
        }
        .sort_box ul {
			overflow: hidden;
        }
        .sort_box ul li {
        	width: 33.333333%;
        	height: 42px;
        	line-height: 42px;
        	float: left;
        	color: #999;
        	font-size: 14px;
        	box-sizing: border-box;
        	display: -webkit-box;
        	-webkit-box-align: center;
        	-webkit-box-pack: center;
        	position: relative
        }
        .sort_box ul li img {
        	height: 12px;
        	margin-right: 4px;
        	display: block;
        }
        .sort_box ul li span {
        	width: 1px;
        	height: 17px;
        	display: block;
        	background-color: #999;
        	position: absolute;
        	right: 0;
        	top: 0;
        	bottom: 0;
        	margin: auto;
        }
        .sort_box ul li.active {
        	color: #e62442;
        }
        .main {
        	background-color: #fff;
        }
        .main ul li .li_box {
			padding: 10px 15px;
			box-sizing: border-box;
        	border-bottom: 1px solid #e8e8e8;
        }
        .main ul li .li_box .li_boxt {
        	display: -webkit-box;
        	display: -webkit-flex;
        }
        .main ul li .li_box .li_box {
            margin-top: 10px;
        }
        .li_img {
        	width: 82px;
        	height: 59px;
        	border-radius: 5px;
        	overflow: hidden;
        }
        .li_img img {
        	width: 100%;
        	height: 100%;
        	object-fit: cover;
        }
        .li_info {
        	padding-left: 10px;
        	box-flex:1;
			-webkit-box-flex:1; 
			flex:1;
			-webkit-flex:1; 
        }
        .li_info .title_box {
        	overflow: hidden;
        	margin-bottom: 3px;
        }
        .li_info .title_box .title {
        	float: left;
        	font-size: 16px;
        	color: #333;
        	overflow: hidden;
        	text-overflow: ellipsis;
        	white-space: nowrap;
        }
        .li_info .title_box .send {
        	float: right;
        	color: #999;
        	font-size: 12px;
        }
        .li_info .evaluate {
        	display: -webkit-box;
        	display: -webkit-flex;
        	align-items: center;
        	margin-bottom: 3px;
        }
        .li_info .evaluate .evaluate_img {
        	overflow: hidden;
        	margin-right: 3px;
        }
        .li_info .evaluate .evaluate_img img {
        	width: 9px;
        	float: left;
        	margin-right: 3px;
        }
        .li_info .evaluate .evaluate_img img:last-child {
        	margin-right: 0;
        }
        .li_info .evaluate .evaluate_num {
        	font-size: 12px;
        	margin-right: 14px;
        	color: #999;
        }
        .li_info .evaluate .month_sell {
        	flex: 1;
        	-webkit-flex: 1;
        	box-flex: 1;
        	-webkit-box-flex: 1;
        	font-size: 12px;
        	color: #999;
        }
        .li_info .distance {
        	font-size: 12px;
        	color: #999;
        }
        .li_discount {
        	font-size: 12px;
        	color: #999;
        }
        .li_discount span {
        	width: 16px;
        	height: 16px;
        	line-height: 16px;
        	text-align: center;
        	background-color: #ff5f78;
        	color: #fff;
        	display: inline-block;
        	border-radius: 3px;
        	margin-right: 5px;
        }
        .main {
            padding-top: 42px;
        }
        .hidden_img {
            display: none;
        }
        .loading{
            color: #ccc;
            text-align: center;
            line-height: 25px;
            display: none;
            background: #f8f8f8;
        }
        .without_data{
            text-align: center;
            display: none;
            background: #f8f8f8;
            color: #999;
            line-height: 30px;
        }
    </style>
</head>
<body>
    <div class="sort_box">
    	<ul>
    		<li class="active" onclick="fnSort('distance',this)" tapmode>距离排序<span></span></li>
    		<li onclick="fnSort('score',this)" tapmode>评价排序<span></span></li>
    		<li tapmode onclick="fnSellerScreen();"><img src="../image/screen.png">筛选</li>
    	</ul>
    </div>
    <div class="main">
    	<ul id="shop_ul">
<!--     		<li onclick="fnOpenSeller();" tapmode>
    			<div class="li_box">
    				<div class="li_boxt">
    					<div class="li_img">
    						<img src="../image/temp/1.png">
    					</div>
    					<div class="li_info">
    						<div class="title_box">
	    						<div class="title">商家名称</div>
	    						<div class="send">20元起送</div>
    						</div>
    						<div class="evaluate">
								<div class="evaluate_img">
									<img src="../image/evaluate_on.png">
									<img src="../image/evaluate_on.png">
									<img src="../image/evaluate_on.png">
									<img src="../image/evaluate_on.png">
									<img src="../image/evaluate_off.png">
								</div>
								<div class="evaluate_num">4.6</div>
								<div class="month_sell">月售109单</div>
    						</div>
    						<div class="distance">距离0.43km | 配送费 6元</div>
    					</div>
    				</div>
    				<div class="li_boxb">
    					<div class="li_discount"><span>减</span>满30减10，满40减15</div>
    				</div>
    			</div>
    		</li> -->
    	</ul>
        <div class="without_data">暂无数据</div>
        <div class="loading">loading...</div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/script.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script id="shopTpl" type="text/x-dot-template">
    {{ for(var i=0;i<it.length;i++){ }}
        <li onclick="fnOpenSeller({{=it[i]['id']}});" tapmode>
            <div class="li_box">
                <div class="li_boxt">
                    <div class="li_img">
                        <img src="../image/default.png">
                        <img onload="setImg(this);" class="hidden_img" src="{{=formatVideoImg(it[i]['img'])}}">
                    </div>
                    <div class="li_info">
                        <div class="title_box">
                            <div class="title">{{=it[i]['name']}}</div>
                            <div class="send">RM{{=it[i]['start_delivery']}}起送</div>
                        </div>
                        <div class="evaluate">
                            <div class="evaluate_img">
                                {{if(it[i]['full_star'] > 0){  }}
                                    {{ for(var j=0;j<it[i]['full_star'];j++){ }}
                                        <img src="../image/evaluate_on.png">
                                    {{ }; }}
                                {{ }; }}
                                {{if(it[i]['half_star'] > 0){  }}
                                    <img src="../image/evaluate_half.png">
                                {{ }; }}
                                {{if(it[i]['off_star'] > 0){  }}
                                    {{ for(var j=0;j<it[i]['off_star'];j++){ }}
                                        <img src="../image/evaluate_off.png">
                                    {{ }; }}
                                {{ }; }}

                            </div>
                            <div class="evaluate_num">{{=it[i]['score_format']}}</div>
                            <div class="month_sell">月售{{=it[i]['month_sale']}}单</div>
                        </div>
                        <div class="distance">距离{{=it[i]['distance']}}km | 配送费RM {{=it[i]['express']}}</div>
                    </div>
                </div>
                {{if(it[i]['coupon_str']){ }}
                <div class="li_boxb">
                    <div class="li_discount"><span>减</span>{{=it[i]['coupon_str']}}</div>
                </div>
                {{ }; }}
            </div>
        </li>
    {{ }; }}
</script>
<script type="text/javascript">
    apiready = function(){
        cid = api.pageParam.cid||1;
        canLoad = true;
        p = 1;
        coupon = '';
        ticket = '';
        price = '';
        shop_type = '';
        order = 'distance';
        height = api.pageParam.height;
        lat = $api.getStorage('now_lat');
        lng = $api.getStorage('now_lng');
        if(lat && lng) {
            openLoading();
            fnGetShop();
        }else {
            api.toast({
                msg: '请先开启定位',
                location : 'middle'
            });
        }
        //上拉加载
        api.addEventListener({
            name:'scrolltobottom',
            extra:{
                threshold:0            //设置距离底部多少距离时触发，默认值为0，数字类型
            }
        }, function(ret, err){
            if(canLoad) {
                p++;
                if($(window).scrollTop() > 0) {
                    $(".loading").show();
                }
                fnGetShop(2);
            }
        });

        //下拉刷新
        api.setCustomRefreshHeaderInfo({
            bgColor: '#f8f8f8',
            loadAnimInterval:60,
            image: {
               pull: pullArr,
               load: loadArr
            }
        }, function(ret, err) {
            p = 1;
            canLoad = true;
            fnGetShop(1);
        });

        //监听筛选
        api.addEventListener({
            name: 'choseAttrEvent'
        }, function(ret, err) {
            if (ret) {
                coupon = ret.value.coupon;
                price = ret.value.price;
                shop_type = ret.value.shop_type;
                ticket = ret.value.ticket;
                canLoad = true;
                p = 1;
                openLoading();
                fnGetShop(1);
            }
        });

    };

    function fnSellerScreen() {
        api.setFrameGroupAttr({
            name: 'sellerList',
            scrollEnabled: false
        });
        api.openFrame({
            name: 'seller_screen_mask',
            url: 'seller_screen_mask.html',
            rect: {
                x: 0,
                y: height + $('.sort_box').height(),
                w: 'auto',
                h: api.winHeight - height - $('.sort_box').height()
            },
            bounces: false
        });
        api.openFrame({
            name: 'seller_screen_frame',
            url: 'seller_screen_frame.html',
            rect: {
                x: 0,
                y: height + $('.sort_box').height(),
                w: 'auto',
                h: api.winHeight - height - $('.sort_box').height() - 80
            },
            bounces: true,
            pageParam: {
                coupon : coupon,
                price : price,
                shop_type : shop_type,
                ticket : ticket
            }
        });
    }

    function fnGetShop(type) {
        if(canLoad == false) {
            return;
        }
        canLoad = false;
        api.ajax({
            url: rootPath+'/api/shop/get_shops',
            method: 'get',
            cache: true,
            data: {
                values: { 
                    p:p,
                    cid : cid,
                    order : order,
                    price : price,
                    coupon:coupon,
                    ticket: ticket,
                    shop_type : shop_type,
                    lat:lat,
                    lng:lng
                },
            }
        }, function(ret, err) {
            //type=1 下拉刷新，清除原来数据
            if(type == 1) {
                $('#shop_ul').empty();
            }
            if (ret) {
                if(ret.status == 200) {
                    $('.without_data').hide();
                    fnSetShop(ret.data);
                }else {
                    if(p == 1) {
                        $('.without_data').show();
                    }
                }
                if(ret.count >= 10) {
                    canLoad == true;
                }
            } else {
                showError(err.code,err.msg)
            }
            if(type == 1) {
                // 是下拉刷新的，要收回
                api.refreshHeaderLoadDone();
            }
            if(type == 2) {
                //上拉给点假时间,好看些         
                setTimeout(function(){
                    $(".loading").hide();
                },1000);
            }else {
                $('.loading').hide();
            }
            closeLoading();
        });
    }

    function fnSetShop(data_) {
        var listT = $api.byId('shopTpl');
        var listTFn = doT.template(listT.text);
        var html = listTFn(data_);
        var list = $api.byId('shop_ul');
        $api.append(list, html);
        api.parseTapmode();
    }

    function fnSort(sort,this_) {
        if(order != sort) {
            openLoading();
            $('.sort_box li').removeClass('active');
            $(this_).addClass('active');
            order  = sort;
            p = 1;
            canLoad = true;
            fnGetShop(1);
        }
    }
</script>
</html>